package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.ArtInformation;
import com.web.exiaodao.remote.vo.ArtInformationVo;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.ArtInformationRepository;
import com.web.exiaodao.remote.service.ApiArtInformationService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2016-11-03 12:08:45
 * 类的说明：前端APIArtInformation接口实现类
 **/
@Service("ApiArtInformationService")
@Transactional(readOnly = false)
public class ApiArtInformationServiceImpl extends BaseService<ArtInformation> implements ApiArtInformationService{

	private static final Logger LOGGER = Logger.getLogger(ApiArtInformationServiceImpl.class);

	@Autowired
	ArtInformationRepository artInformationRepository;
	
	@Override
	public XaResult<ArtInformationVo> findOne(Long tId) throws BusinessException {
		ArtInformation obj = artInformationRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<ArtInformationVo> xr = new XaResult<ArtInformationVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),ArtInformationVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->ArtInformation");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<ArtInformationVo> findLastOne() throws BusinessException {
		XaResult<ArtInformationVo> xr = new XaResult<>();
		Map<String, SearchFilter> filters = new HashMap<>();
			filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
			List<ArtInformation> entitys = artInformationRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ArtInformation.class), new Sort(
				new Sort.Order[]{
				new Sort.Order(Sort.Direction.DESC, "createTime"),
				new Sort.Order(Sort.Direction.DESC, "id")
				}));
				if (entitys == null || entitys.size() == 0) {
				xr.error("暂无信息");
				} else {
				xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ArtInformationVo.class));
				xr.getObject().setId(entitys.get(0).getId());
				LOGGER.info("查询单个实体----->ArtInformation");
			}
		return xr;
	}

	@Override
	public XaResult<List<ArtInformationVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ArtInformation> page = artInformationRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ArtInformation.class), pageable);
		XaResult<List<ArtInformationVo>> xr = new XaResult<List<ArtInformationVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ArtInformationVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<ArtInformationVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ArtInformation> page = artInformationRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ArtInformation.class), pageable);
		XaResult<List<ArtInformationVo>> xr = new XaResult<List<ArtInformationVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), ArtInformationVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<ArtInformationVo> multiOperate(String modelIds,
			Integer status) throws BusinessException {
		XaResult<ArtInformationVo> xr = new XaResult<ArtInformationVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ArtInformation obj = artInformationRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = artInformationRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ArtInformationVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->ArtInformation");
		return xr;
	}

	@Override
	public XaResult<ArtInformationVo> createModel(ArtInformation model)
			throws BusinessException {
		XaResult<ArtInformationVo> xr = new XaResult<ArtInformationVo>();
		ArtInformation obj = artInformationRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ArtInformationVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->ArtInformation");
		return xr;
	}

	@Override
	public XaResult<ArtInformationVo> updateModel(ArtInformation model)
			throws BusinessException {
		ArtInformation obj = artInformationRepository.findOne(model.getId());
		XaResult<ArtInformationVo> xr = new XaResult<ArtInformationVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setAuthorName(XaUtil.isNotEmpty(model.getAuthorName()) ? model.getAuthorName() : obj.getAuthorName());
					obj.setAuthorSex(XaUtil.isNotEmpty(model.getAuthorSex()) ? model.getAuthorSex() : obj.getAuthorSex());
					obj.setAuthorAge(XaUtil.isNotEmpty(model.getAuthorAge()) ? model.getAuthorAge() : obj.getAuthorAge());
					obj.setAuthorTitle(XaUtil.isNotEmpty(model.getAuthorTitle()) ? model.getAuthorTitle() : obj.getAuthorTitle());
					obj.setAuthorHobby(XaUtil.isNotEmpty(model.getAuthorHobby()) ? model.getAuthorHobby() : obj.getAuthorHobby());
					obj.setAuthorIconPic(XaUtil.isNotEmpty(model.getAuthorIconPic()) ? model.getAuthorIconPic() : obj.getAuthorIconPic());
					obj.setAuthorIntroduce(XaUtil.isNotEmpty(model.getAuthorIntroduce()) ? model.getAuthorIntroduce() : obj.getAuthorIntroduce());
				obj = artInformationRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), ArtInformationVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->ArtInformation");
		return xr;
	}

}
